<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼盘名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入楼盘名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社区类型" prop="communityType">
        <el-select v-model="queryParams.communityType" placeholder="请选择社区类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="建筑类型" prop="buildingType">
        <el-select v-model="queryParams.buildingType" placeholder="请选择建筑类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="物业类型" prop="premisesType">
        <el-select v-model="queryParams.premisesType" placeholder="请选择物业类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="纬度" align="center" prop="id" />
      <el-table-column label="纬度" align="center" prop="outsideId" />
      <el-table-column label="楼盘名称" align="center" prop="name" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="county" />
      <el-table-column label="商圈" align="center" prop="commercialArea" />
      <el-table-column label="商圈" align="center" prop="subway" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="建筑面积" align="center" prop="proportion" />
      <el-table-column label="容积率" align="center" prop="grossPlotRatio" />
      <el-table-column label="绿化率" align="center" prop="greenCoverage" />
      <el-table-column label="楼栋数" align="center" prop="buildingCount" />
      <el-table-column label="得房率" align="center" prop="efficiency" />
      <el-table-column label="开发商" align="center" prop="developer" />
      <el-table-column label="物业管理公司" align="center" prop="estate" />
      <el-table-column label="楼盘简介" align="center" prop="introduce" />
      <el-table-column label="社区类型" align="center" prop="communityType" />
      <el-table-column label="建筑类型" align="center" prop="buildingType" />
      <el-table-column label="物业类型" align="center" prop="premisesType" />
      <el-table-column label="经度" align="center" prop="longItude" />
      <el-table-column label="纬度" align="center" prop="latItude" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改楼盘信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="纬度" prop="outsideId">
          <el-input v-model="form.outsideId" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="楼盘名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入楼盘名称" />
        </el-form-item>
<!--        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="county">
          <el-input v-model="form.county" placeholder="请输入区" />
        </el-form-item>-->
        <el-form-item label="省" prop="province">
          <el-select v-model="form.province" placeholder="请选择" @change="getShi(),getSubwayList()">
            <el-option v-for="sheng in shengList" :label="sheng.regionName" :value="sheng.regionId" />
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-select v-model="form.city" placeholder="请选择" @change="getXian()">
            <el-option v-for="shi in shiList" :label="shi.regionName" :value="shi.regionId" />
          </el-select>
        </el-form-item>
        <el-form-item label="区" prop="county">
          <el-select v-model="form.county" placeholder="请选择" @change="getShangQuan()">
            <el-option v-for="xian in xianList" :label="xian.regionName" :value="xian.regionId" />
          </el-select>
        </el-form-item>
        <el-form-item label="商圈" prop="commercialArea">
            <!--<el-input v-model="form.commercialArea" placeholder="请输入商圈" />-->
          <el-checkbox-group v-model="commercialArea">
            <el-checkbox v-for="item in commercialAreaList" :label="item.name" value="item.code" name="commercialArea"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="地铁" prop="subway">
          <el-checkbox-group v-model="subway">
            <el-checkbox v-for="item in subwayList" :label="item.subway" value="item.id" name="subway"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="建筑面积" prop="proportion">
          <el-input v-model="form.proportion" placeholder="请输入建筑面积" />
        </el-form-item>
        <el-form-item label="容积率" prop="grossPlotRatio">
          <el-input v-model="form.grossPlotRatio" placeholder="请输入容积率" />
        </el-form-item>
        <el-form-item label="绿化率" prop="greenCoverage">
          <el-input v-model="form.greenCoverage" placeholder="请输入绿化率" />
        </el-form-item>
        <el-form-item label="楼栋数" prop="buildingCount">
          <el-input v-model="form.buildingCount" placeholder="请输入楼栋数" />
        </el-form-item>
        <el-form-item label="得房率" prop="efficiency">
          <el-input v-model="form.efficiency" placeholder="请输入得房率" />
        </el-form-item>
        <el-form-item label="开发商" prop="developer">
          <el-input v-model="form.developer" placeholder="请输入开发商" />
        </el-form-item>
        <el-form-item label="物业管理公司" prop="estate">
          <el-input v-model="form.estate" placeholder="请输入物业管理公司" />
        </el-form-item>
        <el-form-item label="楼盘简介" prop="introduce">
          <el-input v-model="form.introduce" placeholder="请输入楼盘简介" />
        </el-form-item>
        <el-form-item label="社区类型" prop="communityType">
          <el-select v-model="form.communityType" placeholder="请选择社区类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="建筑类型" prop="buildingType">
          <el-select v-model="form.buildingType" placeholder="请选择建筑类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="物业类型" prop="premisesType">
          <el-select v-model="form.premisesType" placeholder="请选择物业类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="经度" prop="longItude">
          <el-input v-model="form.longItude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latItude">
          <el-input v-model="form.latItude" placeholder="请输入纬度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  provinceList,
  subwayList,
  businessList
} from "@/api/house/info";

export default {
  name: "Info",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼盘信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //商圈列表
      commercialAreaList:[],
      commercialArea:[],
      //地铁
      subwayList:[],
      subway:[],
      //省
      shengList:{},
      //市
      shiList:[],
      //县
      xianList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outsideId: null,
        name: null,
        province: null,
        city: null,
        county: null,
        commercialArea: null,
        subway: null,
        address: null,
        proportion: null,
        grossPlotRatio: null,
        greenCoverage: null,
        buildingCount: null,
        efficiency: null,
        developer: null,
        estate: null,
        introduce: null,
        communityType: null,
        buildingType: null,
        premisesType: null,
        longItude: null,
        latItude: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getShengList();
  },
  methods: {

    /**  获取省的集合  */
    getShengList() {
      let parentId=1;
      provinceList({parentId:parentId}).then(res=>{
        console.log(res)
        this.shengList=res.data
      })
    },
    /** 获取省对应的市集合 */
    getShi() {
      let parentId=this.form.province;
      provinceList({parentId:parentId}).then(res=>{
        console.log(res)
        this.shiList=res.data
      })
    },
    /** 获取市对应的县集合 */
    getXian() {
      let parentId=this.form.city;
      provinceList({parentId:parentId}).then(res=>{
        console.log(res)
        this.xianList=res.data
      })
    },
    /**  获取商圈  */
    getShangQuan() {
      let county=this.form.county;
      businessList({county:county}).then(res=>{
        console.log(res)
        this.commercialAreaList=res.data
      })
    },
    /** 获取区 对应的地铁 */
    getSubwayList() {
      let city=this.form.province;
      subwayList({city:city}).then(res=>{
        this.subwayList=res.data
      })
    },
    /** 查询楼盘信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outsideId: null,
        name: null,
        province: null,
        city: null,
        county: null,
        commercialArea: null,
        subway: null,
        address: null,
        proportion: null,
        grossPlotRatio: null,
        greenCoverage: null,
        buildingCount: null,
        efficiency: null,
        developer: null,
        estate: null,
        introduce: null,
        communityType: null,
        buildingType: null,
        premisesType: null,
        updateTime: null,
        longItude: null,
        latItude: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼盘信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼盘信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除楼盘信息编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house/info/export', {
        ...this.queryParams
      }, `system_info.xlsx`)
    }
  }
};
</script>
